package cn.edu.xjtu.android.emoji_warfare_android.engine;

import java.util.Random;

import cn.edu.xjtu.android.emoji_warfare_android.engine.entity.MovableEntity;
import cn.edu.xjtu.android.emoji_warfare_android.engine.shape.Shape;

/**
 * 掉落物品
 * 
 * @author bill
 *
 */
public class Item extends MovableEntity {
    /**
     * 图像绘制边界
     */
    private ScreenBoundary boundary;
    private float dx;
    private float dy;

    public Item(float birthX, float birthY, Shape outline, ScreenBoundary boundary) {
        super(birthX, birthY, outline);
        this.boundary = boundary;
    }

    /**
     * 随机向一个方向移动
     * 
     * @param speed
     *            速度
     */
    public void randomDirect(float speed) {
        if (r.nextInt(2) == 0) {
            dx = rate * speed;
        } else {
            dx = -rate * speed;
        }
        if (r.nextInt(2) == 0) {
            dy = rate * speed;
        } else {
            dy = -rate * speed;
        }
    }

    @Override
    public boolean move() {
        int flag = boundary.range(this);
        // 调整方向
        if ((flag & 10) > 0) { // 10 = bx1010
            dy = -dy;
        }
        if ((flag & 5) > 0) { // 5 = bx0101
            dx = -dx;
        }
        basePoint.x += dx;
        basePoint.y += dy;
        return false;
    }

    private static float rate;
    private static Random r;
    static {
        r = new Random(System.currentTimeMillis());
        rate = (float) (Math.sqrt(2) / 2);
    }
}
